package cn.sys.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.sys.constast.SysConstast;
import cn.sys.domain.User;
import cn.sys.service.LogService;
import cn.sys.service.UserService;
import cn.sys.utils.WebUtils;
import cn.sys.vo.LogVo;
import cn.sys.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

/**
 * @author Mxb
 * @version 1.0
 * @date 2019/10/27 18:15
 * 用户登录控制器
 */
@Controller
@RequestMapping("login")
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private LogService logService;

    /**
     * 跳转系统后台首页
     * @return
     */
    @RequestMapping("toMain")
    public String toMain() {
        return "sys/main/index";
    }


    /**
     * 跳转登录界面
     * @return
     */
    @RequestMapping("toLogin")
    public String toLogin() {
        return "sys/main/login2";
    }

    /**
     * 登录方法
     * @param userVo
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("login")
    public String login(UserVo userVo, Model model, HttpSession session) {
        String code = WebUtils.getHttpSession().getAttribute("code").toString();
        if (userVo.getCode().equals(code)) {
            User user = this.userService.login(userVo);
            if (null != user) {
                //放到session中
                WebUtils.getHttpSession().setAttribute("user", user);
                //记录登录日志
                LogVo logInfoVo = new LogVo();
                logInfoVo.setLoginname(user.getLoginname() + "-" + user.getRealname());
                logInfoVo.setLogintime(new Date());
                // ip如何获取 从请求中拿
                logInfoVo.setLoginip(WebUtils.getHttpServletRequest().getRemoteAddr());
                logService.addLog(logInfoVo);

                //跳转页面
                return "redirect:toMain.action";
            } else {
                model.addAttribute("error", SysConstast.USER_LOGIN_ERROR_MSG);
                return "sys/main/login2";
            }
        } else {
            model.addAttribute("error", "验证码不正确!");
            return "sys/main/login2";
            //return "redirect:toLogin.action";
        }
    }

    /**
     * 返回登录验证码 信息
     * @param response
     * @param session
     * @throws IOException
     */
    @RequestMapping("getCode")
    public void getCode(HttpServletResponse response, HttpSession session) throws IOException {
        //定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36, 4, 5);
        //图形验证码写出，可以写出到文件，也可以写出到流
        //  lineCaptcha.write("d:/line.png");
        session.setAttribute("code", lineCaptcha.getCode());
        ServletOutputStream outputStream = response.getOutputStream();
        ImageIO.write(lineCaptcha.getImage(), "JPEG", outputStream);
        //System.out.println(lineCaptcha.getImage());
    }

    /**
     * 注销登录
     * @param session
     * @return
     */
    @RequestMapping("logout")
    public String logout(HttpSession session) {
        if (session != null) {
            // 销毁session
            session.invalidate();
        }
        return "redirect:/login/toLogin.action";
    }
}
